import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { isHttp, isEmpty } from "@/utils/validate"
import {login, logout, getUserInfo} from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/static/images/profile.jpg'

const baseUrl = config.baseUrl

const user = {
  state: {
    token: getToken(),
    id: storage.get(constant.id),
    name: storage.get(constant.name),
    number: storage.get(constant.number),
    avatar: storage.get(constant.avatar),
    qrcode: storage.get(constant.qrcode),
    qrcodePath: storage.get(constant.qrcodePath),
    phone: storage.get(constant.phone),
    card: storage.get(constant.card)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
      storage.set(constant.id, id)
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_NUMBER: (state, number) => {
      state.number = number
      storage.set(constant.number, number)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_QRCODE: (state, qrcode) => {
      state.qrcode = qrcode
      storage.set(constant.qrcode, qrcode)
    },
    SET_QRCODE_PATH: (state, qrcodePath) => {
      state.qrcodePath = qrcodePath
      storage.set(constant.qrcodePath, qrcodePath)
    },
    SET_CARD: (state, card) => {
      state.card = card
      storage.set(constant.card, card)
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
      storage.set(constant.phone, phone)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(res => {
          const user = res.data

		  let avatar = user.avatar || ""
		  if (!isHttp(avatar)) {
            avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
          }
          const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
		  const username = (isEmpty(user) || isEmpty(user.nickName)) ? user.phonenumber : user.nickName
          commit('SET_ID', userid)
          commit('SET_NAME',  user.nickName)
          commit('SET_PHONE',  user.phonenumber)

          commit('SET_NUMBER', user.number)
          commit('SET_AVATAR', avatar)
          commit('SET_QRCODE', user.qrcode)
          commit('SET_QRCODE_PATH', user.qrcodePath)
          commit('SET_CARD', user.card)
          console.log(user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_ID', '')
          commit('SET_NAME', '')
          commit('SET_AVATAR', '')
          commit('SET_NUMBER', '')

          removeToken()
          storage.clean()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
